package work.skc.customer_task_record;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import core.util.TransformCsvData;
import io.netty.channel.nio.NioEventLoopGroup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import work.MemberUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *过滤数据
 */
@Slf4j
public class Setup1 {
    @SneakyThrows
    public static void main(String[] args) {
        List<CustomerExcelObject> list = new CopyOnWriteArrayList<>();
        List<String> thiIds = new ArrayList<>();
        AtomicInteger count = new AtomicInteger();

        TransformCsvData.peekCsv(Setting.data, 0, s -> {
            thiIds.add(s[0].trim());
        });
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup(3);

        for (String thiId : thiIds) {
            eventExecutors.execute(()->{
                JSONArray array = MemberUtil.getCustomerListByThiCode(thiId, Setting.baseUrl, Setting.token);
                if (CollUtil.isEmpty(array)) {
                    if (count.getAndIncrement() % 10 == 0) {
                        log.info("【用户任务记录】查询重复 进度 ->{}", count.get());
                    }
                    log.error("【用户任务记录】is null->{}", thiId);
                    list.add(CustomerExcelObject.form(thiId));
                    return;
                }

                int size = array.size();
                for (int i = 0; i < size; i++) {

                    if (count.getAndIncrement() % 10 == 0) {
                        log.info("【用户任务记录】查询重复 进度 ->{}", count.get());
                    }

                    JSONObject jsonObject = array.getJSONObject(i);
                    boolean isMulti = size > 1;
                    if (isMulti) {
                        log.info("is mult ->{}", jsonObject);
                    }
                    list.add(CustomerExcelObject.form(jsonObject, isMulti));
                }
            });

        }
        eventExecutors.shutdownGracefully().sync();
        EasyExcel.write(Setting.setup1, CustomerExcelObject.class).sheet("sheet1").doWrite(list);
    }
}
